package Persistance;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import Model.*;


public class ItemMineralLoteDataMapper implements IDataMapper<ItemMineralLote>
{

	public boolean insert(ItemMineralLote itMinlote) 
	{
		Connection conn = PoolConnection.getPoolConnection().getConnection();
		
		try 
		{
			PreparedStatement s = conn.prepareStatement("insert into itemMineralLote values (?,?,?)");
			s.setString(1, itMinlote.getMineral().getCodigo());
			s.setInt(2, itMinlote.getNroLote());
			s.setFloat(3, itMinlote.getPorcentaje());
			
			int rowCount = s.executeUpdate();
			return rowCount == 1;
		} 
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
			return false;
		}
		finally
		{
			PoolConnection.getPoolConnection().realeaseConnection(conn);
		}
	}

	public boolean update(ItemMineralLote itMinlote) 
	{
		Connection conn = PoolConnection.getPoolConnection().getConnection();
		
		try 
		{
			PreparedStatement s = conn.prepareStatement("update itemMineralLote set porcentaje = ? "
														+ " where codMineral = ? and nroLote = ?");
			s.setFloat(1, itMinlote.getPorcentaje());
			s.setString(2, itMinlote.getMineral().getCodigo());
			s.setInt(3, itMinlote.getNroLote());
			
			int rowCount = s.executeUpdate();
			return rowCount == 1;
		} 
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
			return false;
		}
		finally
		{
			PoolConnection.getPoolConnection().realeaseConnection(conn);
		}
	}

	public boolean delete(ItemMineralLote itMinlote) 
	{
		Connection conn = PoolConnection.getPoolConnection().getConnection();
		
		try 
		{
			PreparedStatement s = conn.prepareStatement("delete from itemMineralLote where codMineral = ? and nroLote = ?");
			s.setString(1, itMinlote.getMineral().getCodigo());
			s.setInt(2, itMinlote.getNroLote());
			
			int rowCount = s.executeUpdate();
			return rowCount == 1;
		} 
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
			return false;
		}
		finally
		{
			PoolConnection.getPoolConnection().realeaseConnection(conn);
		}
	}

	public List<ItemMineralLote> findAll() 
	{
		return null;
	}

	public ItemMineralLote findById(ItemMineralLote o) 
	{
		return null;
	}

}